package com.ruoyi.cms.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.cms.domain.CmsCatalog;
import com.ruoyi.cms.service.ICmsCatalogService;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CmsNavigationMapper;
import com.ruoyi.cms.domain.CmsNavigation;
import com.ruoyi.cms.service.ICmsNavigationService;

import javax.annotation.PostConstruct;

/**
 * 页面导航Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-01
 */
@Service
public class CmsNavigationServiceImpl implements ICmsNavigationService 
{
    private static final String CACHE_KEY = "cms_navigation";

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private CmsNavigationMapper cmsNavigationMapper;
    @Autowired
    private ICmsCatalogService cmsCatalogService;


    @PostConstruct
    public  int recache() {
        redisCache.deleteObject(CACHE_KEY);
        List<CmsNavigation> list = cmsNavigationMapper.selectCmsNavigationList(null);
        for(CmsNavigation navigation:list){
            redisCache.setCacheMapValue(CACHE_KEY, "id_"+navigation.getId(), navigation);
        }
        return 0;
    }

    /**
     * 查询页面导航列表
     *
     * @return 页面导航集合
     */
    public List<CmsNavigation> listNavigation(){
        Map<String, CmsNavigation> navigationMap = redisCache.getCacheMap(CACHE_KEY);
        List<CmsNavigation> tops = navigationMap.values().stream().sorted(Comparator.comparingInt(CmsNavigation::getSorts)).collect(Collectors.toList());
        for(CmsNavigation navigation:tops){
            if(navigation.getType().equalsIgnoreCase(CmsNavigation.TYPE_CATALOG)){
                CmsCatalog catalog = cmsCatalogService.selectCmsCatalogById(Long.parseLong(navigation.getUri()));
                if(catalog.getType().equalsIgnoreCase("node")){
                    CmsCatalog cc = new CmsCatalog();
                    cc.setPid(catalog.getId());
                    List<CmsCatalog> subList = cmsCatalogService.selectCmsCatalogList(cc);
                    for(CmsCatalog sc:subList){
                        CmsNavigation nav = new CmsNavigation();
                        nav.setUri("nav-"+navigation.getId()+"-"+sc.getId()+".html");
                        nav.setTitle(sc.getTitle());
                        nav.setTarget(navigation.getTarget());
                        nav.setType(CmsNavigation.TYPE_CATALOG);
                        navigation.addChildren(nav);
                    }
                }
                navigation.setUri("nav-"+navigation.getId()+".html");
            }else if(navigation.getType().equalsIgnoreCase(CmsNavigation.TYPE_FORM)){

                navigation.setUri("nav-"+navigation.getId()+".html");
            }
        }
        return tops;
    }

    /**
     * 查询页面导航
     * 
     * @param id 页面导航主键
     * @return 页面导航
     */
    @Override
    public CmsNavigation selectCmsNavigationById(Long id)
    {
        return redisCache.getCacheMapValue(CACHE_KEY, "id_"+id);
    }

    /**
     * 查询页面导航列表
     * 
     * @param cmsNavigation 页面导航
     * @return 页面导航
     */
    @Override
    public List<CmsNavigation> selectCmsNavigationList(CmsNavigation cmsNavigation)
    {
        return cmsNavigationMapper.selectCmsNavigationList(cmsNavigation);
    }

    /**
     * 新增页面导航
     * 
     * @param cmsNavigation 页面导航
     * @return 结果
     */
    @Override
    public int insertCmsNavigation(CmsNavigation cmsNavigation)
    {
        return cmsNavigationMapper.insertCmsNavigation(cmsNavigation) + recache();
    }

    /**
     * 修改页面导航
     * 
     * @param cmsNavigation 页面导航
     * @return 结果
     */
    @Override
    public int updateCmsNavigation(CmsNavigation cmsNavigation)
    {
        return cmsNavigationMapper.updateCmsNavigation(cmsNavigation) + recache();
    }

    /**
     * 批量删除页面导航
     * 
     * @param ids 需要删除的页面导航主键
     * @return 结果
     */
    @Override
    public int deleteCmsNavigationByIds(Long[] ids)
    {
        return cmsNavigationMapper.deleteCmsNavigationByIds(ids) + recache();
    }

    /**
     * 删除页面导航信息
     * 
     * @param id 页面导航主键
     * @return 结果
     */
    @Override
    public int deleteCmsNavigationById(Long id)
    {
        return cmsNavigationMapper.deleteCmsNavigationById(id) + recache();
    }
}
